package com.pw.study.flink.state;

import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.common.state.ListState;
import org.apache.flink.api.common.state.ListStateDescriptor;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.runtime.state.FunctionInitializationContext;
import org.apache.flink.runtime.state.FunctionSnapshotContext;
import org.apache.flink.streaming.api.checkpoint.CheckpointedFunction;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.util.Collector;

import java.util.ArrayList;
import java.util.List;

public class OperatorStateUnionList {

    public static void main(String[] args) {
        stateList();
    }

    private static void stateList() {
        Configuration conf = new Configuration();
        conf.setInteger("rest.port", 20000);
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);
        env.setParallelism(2);
        //开启CheckPoint
        env.enableCheckpointing(3000);
        //输入数据
        env.socketTextStream("hadoop111", 9999).flatMap(new MyFlatMapUnion()).print();

        try {
            env.execute();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static class MyFlatMapUnion implements FlatMapFunction<String, String>, CheckpointedFunction {
        //暂存数据
        List<String> words = new ArrayList<String>();
        private ListState<String> listState;

        @Override
        public void flatMap(String value, Collector<String> out) throws Exception {
            if (value.contains("exit")) {
                throw new RuntimeException(" 重启。。。，程序重新加载。。。。");
            }

            String[] line = value.split(" ");
            for (String s : line) {
                words.add(s);
            }
            out.collect(words.toString());

        }

        @Override
        public void initializeState(FunctionInitializationContext ctx) throws Exception {
            System.out.println("init.......");
            listState = ctx.getOperatorStateStore().getUnionListState(new ListStateDescriptor<String>("listState", String.class));
            //加载状态中的数据
            for (String s : listState.get()) {
                words.add(s);
            }
        }

        @Override
        public void snapshotState(FunctionSnapshotContext ctx) throws Exception {
            //System.out.println("save...");
            listState.update(words);
        }
    }
}
